package com.jxpanda.infrastructure.core.formatter;

import com.jxpanda.infrastructure.core.constant.DateTimeConstant;
import lombok.Getter;

import java.text.ParsePosition;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalQuery;

/**
 * 日期格式化的枚举类
 * 枚举常用的日期格式
 *
 * @author Panda
 */
@Getter
public enum DateTimeFormatter {
    /**
     * yyyy-MM-dd HH:mm:ss
     */
    DATETIME(DateTimeConstant.PATTERN_DATETIME),
    /**
     * yyyy-MM-dd
     */
    DATE(DateTimeConstant.PATTERN_DATE),
    /**
     * HH:mm:ss
     */
    TIME(DateTimeConstant.PATTERN_TIME),
    /**
     * yyyyMMddHHmmss
     */
    DATETIME_COMPACT(DateTimeConstant.PATTERN_DATETIME_COMPACT),
    /**
     * yyyyMMdd
     */
    DATE_COMPACT(DateTimeConstant.PATTERN_DATE_COMPACT),
    /**
     * HH:mm
     */
    TIME_HH_MM("HH:mm");

    private final String pattern;
    private final java.time.format.DateTimeFormatter formatter;

    DateTimeFormatter(String pattern) {
        this.pattern = pattern;
        this.formatter = java.time.format.DateTimeFormatter.ofPattern(pattern);
    }

    public String format(TemporalAccessor temporalAccessor) {
        return this.formatter.format(temporalAccessor);
    }

    public TemporalAccessor parse(CharSequence text) {
        return this.formatter.parse(text);
    }

    public <T> T parse(String string, TemporalQuery<T> query) {
        return this.formatter.parse(string, query);
    }

}
